
let UIConstants = require('../ui_constants');
let BkpSubItemView = require('./BkpSubItemView');
let BaseView = require('../BaseView');

class BkpItemView extends BaseView{

    constructor() {
        super();

        // setup rootEl
        this.rootEl = document.createElement('div');
        // this.rootEl.classList.add('bkpItem');
        this.rootEl.classList.add('border');

        //
        this._start = new BkpSubItemView(UIConstants.BkpSubItemType.Begin);
        this.addView(this._start);

        this._end = new BkpSubItemView(UIConstants.BkpSubItemType.End);
        this.addView(this._end);
    }

    get start(){
        return this._start;
    }

    get end(){
        return this._end;
    }

    onUpdateIndex(type,newAddValue){
        if(type === UIConstants.BkpSubItemType.Begin){
            this.parentView.updateBeginIdx(this,newAddValue);
        }else if(type === UIConstants.BkpSubItemType.End){
            this.parentView.updateEndIdx(this,newAddValue);
        }else{
            throw new Error(`error type:${type}`);
        }
    }

    onDeleteItem(){
        this.parentView.deleteItem(this);
    }
    onChangeBkpType(newBkpType){
        this.parentView.changeBkpType(this,newBkpType);
    }

    onChangeIdx(type,newIdx){
        if(type === UIConstants.BkpSubItemType.Begin){
            this.parentView.changeBeginIdx(this,newIdx);
        }else if(type === UIConstants.BkpSubItemType.End){
            this.parentView.changeEndIdx(this,newIdx);
        }else{
            throw new Error(`error type:${type}`);
        }
    }

    updateByModel(itemModel){
        // 更新 begin end idx
        // 添加min max
        this._start.setRange(0,itemModel.totalLen - 1);
        this._end.setRange(0,itemModel.totalLen - 1);

        // 设置当前位置
        this._start.setCurIdx(itemModel.beginIdx);
        this._end.setCurIdx(itemModel.endIdx);

        // 设置bkp类型
        this._start.setBkpType(itemModel.bkpType);
    }
}

module.exports = BkpItemView;